﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
using FreshCommonUtility.Dapper;
using FreshCommonUtility.SqlHelper;
using WeChatCmsCommon.EnumBusiness;
using WeChatModel.CustomerModel;
using WeChatModel.DatabaseModel;

namespace WeChatDataAccess
{
    public class DailyverifydetailData : BaseData<long, DailyverifydetailModel>
    {
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="saveModel"></param>
        public long SaveModel(DailyverifydetailModel saveModel)
        {
            if (saveModel == null) return 0;
            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                if (saveModel.Id < 1)
                {
                    //新增
                    return conn.Insert<long, DailyverifydetailModel>(saveModel);
                }

                //修改
                conn.Update(saveModel);
                return saveModel.Id;
            }
        }

        /// <summary>
        /// 根据审核用户记录
        /// </summary>
        /// <param name="verifyId">审核人用户id</param>
        /// <returns>查询日志记录</returns>
        public List<DailyverifydetailModel> GetDailyVerifyDetail(long verifyId)
        {
            List<DailyverifydetailModel> dataList;
            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                dataList = conn.GetList<DailyverifydetailModel>(new
                { IsDel = 0, VerifyId = verifyId })
                    ?.OrderBy(f => f.Id).ToList();
            }
            return dataList;
        }

        /// <summary>
        /// 根据记录id查询还未审核的记录
        /// </summary>
        /// <param name="dailyId">dailyId</param>
        /// <returns></returns>
        public DailyverifydetailModel GetDailyVerifyDetailByDailyId(long dailyId)
        {
            DailyverifydetailModel dataList;
            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                dataList = conn.GetList<DailyverifydetailModel>(new
                { VerifyState = 0, DailyId = dailyId, IsDel = FlagEnum.HadZore }).SingleOrDefault();
            }
            return dataList;
        }

        /// <summary>
        /// 根据审核人id，审核状态获取申请记录
        /// </summary>
        /// <param name="verifyId">审核人</param>
        /// <param name="verifyStateEnum">审核状态</param>
        /// <returns></returns>
        public List<DailyHistoryWithVerifyModel> GetDailyHistoryModels(long verifyId, VerifyStateEnum verifyStateEnum, int pageIndex, int pageSize)
        {
            var select = @"select h.id,
	h.DailyNumber,
	h.DailyDate,
	h.DailyContent,
	h.Createtime,
	h.UserId,
	h.UpdateTime,
	h.IsDel,
	h.DailyYear,
	h.DailyMonth,
	h.DailyDay,
	h.CreateUserName,
	v.VerifyId,
	v.VerifyState,
	v.verifyStr,
	h.DailyType,
	v.id AS VerifyDetailId  from dailyhistory h join dailyverifydetail v on h.id=v.DailyId ";
            var where = new StringBuilder(" where h.DailyType in(1,0) and v.VerifyId=@VerifyId ");
            if (verifyStateEnum == VerifyStateEnum.ReadyDo)
            {
                where.Append(" and v.VerifyState=@VerifyState and h.IsDel=0 ");
            }
            else
            {
                where.Append(" and( v.VerifyState in (1,2) or h.IsDel = 1 )");
            }
            var oderBy = " ORDER BY v.id desc ";
            var limit = " limit @start,@end ";
            var param = new
            {
                VerifyState = verifyStateEnum.GetHashCode(),
                VerifyId = verifyId,
                start = (pageIndex - 1) * pageSize,
                end = pageIndex * pageSize
            };
            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                IEnumerable<DailyHistoryWithVerifyModel> query = conn.Query<DailyHistoryWithVerifyModel>(select + where + oderBy + limit, param);
                return query.ToList();
            }
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="verifyId"></param>
        /// <param name="verifyStateEnum"></param>
        /// <returns></returns>
        public int GetDailyHistoryModelsCount(long verifyId, VerifyStateEnum verifyStateEnum)
        {
            var select = @"select count(1) from dailyhistory h join dailyverifydetail v on h.id=v.DailyId ";
            var where = new StringBuilder(" where h.DailyType in(1,0) and v.VerifyId=@VerifyId ");
            if (verifyStateEnum == VerifyStateEnum.ReadyDo)
            {
                where.Append(" and v.VerifyState=@VerifyState and h.IsDel=0 ");
            }
            else
            {
                where.Append("  and( v.VerifyState in (1,2) or h.IsDel = 1 )");
            }
            var param = new
            {
                VerifyState = verifyStateEnum.GetHashCode(),
                VerifyId = verifyId
            };
            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                return conn.Query<int>(select + where.ToString(), param).SingleOrDefault();
            }
        }
    }
}
